<?php

/*
	任务列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['types']=array(
	'1'=>'常规模型',
	'2'=>'售后维护',
	'3'=>'服务模型',
);
$return['statuss']=$this->task_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//类型。1表示开发任务，2表示bug修复任务，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if($_RQ['status']==10){
		$params['status']=0;
		$params['getmid']=0;
	}else{
		$params['getmid']=$_SESSION['uid'];
		if(!$_RQ['islog']){
			$params['status']=intval($_RQ['status']);
		}
	}
	$return['params']['status']=intval($_RQ['status']);
	//任务名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_workliu_task',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['groupid']=pdo_getcolumn('jy_workliu_taskgroup',array('id'=>$row['groupid']),'title');
		$row['createtime']=date('Y-m-d',$row['createtime']);
	}
	$return['list']=$list?$list:array();
	$return['receive_types']=array(
		'1'=>'管理员指派',
		'2'=>'自己领取',
		'3'=>'发布者指派',
	);
	exi($return);
}
if($_RQ['op']=='get_work'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$check=pdo_get('jy_workliu_task',array('id'=>$id),array('id'));
	if(!$check){
		exi('指定任务不存在！','error');
	}
	if($check['getmid']){
		exi('任务已被领取！请选择其他任务！','error');
	}
	$update=array(
		'getmid'=>$_SESSION['uid'],
		'status'=>1,
		'receive_type'=>2,//2表示自己领取
		'receive_time'=>TIMESTAMP,
	);
	pdo_update('jy_workliu_task',$update,array('id'=>$id));
	exi('领取成功！','','list');
}
//提交任务
if($_RQ['op']=='complete'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'result'=>trim($post['result']),
			'status'=>2,
			'complete_time'=>TIMESTAMP,
		);
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择任务！','error');
		}
		if(!$data['result']){
			exi('请先填写任务处理结果！','error');
		}
		pdo_update('jy_workliu_task',$data,array('id'=>$id,'getmid'=>$_SESSION['uid']));
		exi('提交成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id,'getmid'=>$_SESSION['uid']),array('id','title','content'));
	if(!$item){
		exi('指定任务不存在！','error');
	}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择工单！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id));
	$log=pdo_getall('jy_workliu_tcplog',array('tcpid'=>$item['tcpid']));
	$item['getname']=pdo_getcolumn('core_members',array('id'=>$item['getmid']),'nickname');
	$item['getname']=$item['getname']?:'匿名';
	if($log){
		foreach($log as &$v){
			if($v['setmid']==$item['setmid']){
				$v['isme']=1;
			}
			$v['createtime']=date('m-d H:i',$v['createtime']);
		}
	}
	$item['image']=$item['image']?json_decode($item['image'],true):array();
	$item['assess'] = $item['assess']? $item['assess'] : '还未审核！';
	$return['item']=$item;
	$return['log']=$log;
	$return['post']=array('taskid'=>$item['id']);
	exi($return);
}
if($_RQ['op']=='addlog'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$taskid=intval($post['taskid']);
		$item=pdo_get('jy_workliu_task',array('id'=>$taskid),array('id','title','tcpid'));
		$tcplog = array(
			'tcpid'=>$item['tcpid'],
			'uniacid'=>$_SESSION['uniacid'],
			'setmid'=>$_SESSION['uid'],
			'msg'=>trim($post['msg']),
			'image'=>ijson_encode($post['image']),
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_workliu_tcplog',$tcplog);
		exi('提交成功！','','list');
	}
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'uniacid'=>intval($_SESSION['uniacid']),
			'groupid'=>intval($post['groupid']),
			'content'=>trim($post['content']),
			'image'=>ijson_encode($post['image']),
			'web'=>trim($post['web']),
			'ftp'=>trim($post['ftp']),
			'db'=>trim($post['db']),
			'credit'=>intval($post['credit']),
			'default_type'=>intval($post['default_type']),
			'service_type'=>intval($post['service_type']),
			'bug_type'=>intval($post['bug_type']),
			'project_type'=>intval($post['project_type']),
			'jiaji'=>intval($post['jiaji']),
			'setmid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_workliu_task','ordersn'),
		);
		$id=intval($post['id']);
		if($id){
			$tcpid = pdo_getcolumn('jy_workliu_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'tcpid');
			$tcplog = array('msg'=>trim($post['content']),'image'=>trim($post['image']));
			pdo_update('jy_workliu_task',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_workliu_tcplog',$tcplog,array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['setmid']=$_SESSION['uid'];
			pdo_insert('jy_workliu_tcp',array('uniacid'=>$_SESSION['uniacid']));
			$tcpid=pdo_insertid();
			$data['tcpid']=$tcpid;
			pdo_insert('jy_workliu_task',$data);
			$tcplog = array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'msg'=>trim($post['content']),'image'=>trim($post['image']),'createtime'=>TIMESTAMP);
			pdo_insert('jy_workliu_tcplog',$tcplog);
		}
		exi('操作成功！','','mylist');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_workliu_task',array('id'=>$id));
		$item['image']=$item['image']?json_decode($item['image'],true):array();
	}else{
		$item=array();
	}
	
	$return['groups']=pdo_getall('jy_workliu_taskgroup',array(),array('id','title'));
	$return['item']=$item;
	exi($return);
}
//指派任务
if($_RQ['op']=='staff'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$item=pdo_get('jy_workliu_task',array('id'=>$id),array('id','title','tcpid'));
		if($item['setmid'] == $_SESSION['uid']){
			$receive_type = 1;
		}else{
			$receive_type = 3;
		}
		$getmid = pdo_getcolumn('jy_scrm_worker',array('id'=>intval($post['getmid'])),'mid');
		$data=array(
			'status'=>1,
			'receive_type'=>$receive_type,//1表示发布者指派
			'receive_time'=>TIMESTAMP,
			'getmid'=>$getmid,
		);
		if(!$id){
			exi('请先选择任务！','error');
		}
		if(!$data['getmid']){
			exi('请先选择处理人！','error');
		}
		pdo_update('jy_workliu_task',$data,array('id'=>$id));
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id),array('id','title','tcpid'));
	if(!$item){
		exi('指定任务不存在！','error');
	}
	$return['workers']=pdo_getall('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'指派特定人员处理任务',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称','read'=>true),
			array('type'=>'select','name'=>'getmid','title'=>'指派处理人','option_title'=>'name','options'=>'workers'),
		),
	);
	exi($return);
}